@echo off
setlocal enabledelayedexpansion

REM 获取当前目录
set "current_dir=%cd%"

if not exist "%current_dir%\build\" (
    REM 构建编译结果目录
    mkdir "%current_dir%\build"
) else (
    echo --------------------------------- Cleaning ---------------------------------{
    :: 清除本层中的Exe
    if exist "%current_dir%\build\*.exe" (
        del /F /Q "%current_dir%\build\*.exe"
        echo Cleaning EXE.
    )
    echo ----------------------------------------------------------------------------}
    echo Cleaning completed.
    echo.
)


REM 判断 cjdoc 是否已经编译

set "cjdoc_binary_en=%current_dir%\target\release\bin\cjdoc_en.exe"
set "cjdoc_binary_zh_cn=%current_dir%\target\release\bin\cjdoc_zh-cn.exe"
set "all_exist=1"
if not exist "%cjdoc_binary_en%" ( set "all_exist=0" )
if not exist "%cjdoc_binary_zh_cn%" ( set "all_exist=0" )

if "%all_exist%"=="1" (
    echo cjdoc has been compiled.

    :: 提示用户输入逻辑值
    :prompt
    set "choiceRC="
    set /p choiceRC="Do you want to recompile? (Y/N):" 

    :: 检查用户输入
    if /i "!choiceRC!"=="Y" (
        goto recompile
    ) else if /i "!choiceRC!"=="N" (
        echo Skip compilation cjdoc.
    ) else (
        echo Invalid value.
        goto prompt
    )
) else (
    :recompile
    echo ----------------------------- Compiling cjdoc -------------------------------{
    cjpm update
    cjpm build --cfg_en -o cjdoc_en.exe
    cjpm build --cfg_zh_cn -o cjdoc_zh-cn.exe
    echo ----------------------------------------------------------------------------}
    echo cjdoc compilation completed.
    echo.
    echo ------------------------------- Copy Result -------------------------------{
    :: 拷贝Exe
    echo "%current_dir%\target\release\bin\"
    cd /d "%current_dir%\target\release\bin\"
    for /r %%i in (*.exe) do (
        echo Copy %%i to "%current_dir%\build"
        copy "%%i" "%current_dir%\build" /Y
    )
    echo ---------------------------------------------------------------------------}
    echo.
)



echo completed...
pause
endlocal